# 导入fastapi包
from fastapi import FastAPI, Request, Form
from fastapi.responses import RedirectResponse
from fastapi.templating import Jinja2Templates
import uvicorn

app = FastAPI()
template = Jinja2Templates("pages")

todos = ["qwq1", "qwq2", "qwq3"]


@app.get("/")
def index(req: Request):
    return template.TemplateResponse("index.html", context={"request": req, "todos": todos})


@app.post("/add_todo")
def add_todo(todo=Form(None)):
    """处理用户发过来的add into todolist 操作"""
    todos.insert(0, todo)
    return RedirectResponse("/", status_code=302)


if __name__ == '__main__':
    uvicorn.run(app)
